if __name__ == '__main__':
    from gSystem import injectMain
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    app = QApplication(['Gestalt'])
    main = injectMain()
    
    import gSystem, gRPC, gViews, gRemote, gEvent, gChat, gMap, gUnit
    
    # Initialize view state.
    s = gViews._state
    s.initialize()
    
    # EVENT WIRING
    # amounts to configuration
    
    # mouse events
    main.glwidget.mouseMoveSignal.connect(gEvent.mouseMoveEvent)
    main.glwidget.mousePressSignal.connect(gEvent.mousePressEvent)
    main.glwidget.mouseReleaseSignal.connect(gEvent.mouseReleaseEvent)
    
    # chat widget
    s.cwidget.chatInput.connect(gEvent.chatInputEvent)
    
    # game control widget
    s.gwidget.endTurn.connect(gViews.sendTurnComplete)
    s.gwidget.domSig.connect(gViews.showDomestic)
    s.gwidget.persSig.connect(gViews.showPersonnel)
    s.gwidget.ecoSig.connect(gViews.showEconomic)
    s.gwidget.diploSig.connect(gViews.showDiplomacy)
    s.gwidget.espSig.connect(gViews.showEspionage)
    
    # menu items
    s.menu.newSessionAct.triggered.connect(gViews.createSession)
    s.menu.hostGameAct.triggered.connect(gViews.hostGame)
    s.menu.joinGameAct.triggered.connect(gViews.joinGame)
    s.menu.disconnectAct.triggered.connect(gViews.disconnectGame)
    
    server = gRPC.server
    client = gRPC.client
    
    client.connected.connect(gRemote.clientConnect)
    client.disconnected.connect(gRemote.clientDisconnect)
    client.objectReceived.connect(gRemote.clientReceive)
    client.fileReceived.connect(gRemote.clientFileReceive)
    server.connected.connect(gRemote.serverConnect)
    server.disconnected.connect(gRemote.serverDisconnect)
    server.objectReceived.connect(gRemote.serverReceive)
    server.fileReceived.connect(gRemote.serverFileReceive)
    
    # Start execution
    try:
        main.show()
        app.exec_()
    finally:
        client.close()
